﻿namespace Sofire.Data
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 表示一个包含 <see cref="Sofire.Data.ExecuteEventObjectBase&lt;TCommand&gt;.Executing"/> 和 <see cref="Sofire.Data.ExecuteEventObjectBase&lt;TCommand&gt;.Executed" /> 的引擎对象基类。
    /// </summary>
    /// <typeparam name="TManager">管理器的类型。</typeparam>
    /// <typeparam name="TCommand">命令的类型。</typeparam>
    public abstract class ExecuteEventObjectEngineBase<TManager, TCommand> : ExecuteEventObjectBase<TCommand>
        where TManager : ExecuteEventObjectBase<TCommand>
        where TCommand : ExecuteCommandBase
    {
        #region Fields

        /// <summary>
        /// 引擎的管理器。
        /// </summary>
        protected internal TManager _Manager;

        /// <summary>
        /// 引擎的唯一标识符。
        /// </summary>
        protected internal string _Name = null;

        #endregion Fields

        #region Properties

        /// <summary>
        /// 获取引擎的管理器。
        /// </summary>
        public TManager Manager
        {
            get
            {
                return this._Manager;
            }
        }

        /// <summary>
        /// 获取引擎的唯一标识符。
        /// </summary>
        public virtual string Name
        {
            get
            {
                return this._Name;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// 表示 <see cref="Sofire.Data.ExecuteEventObjectBase&lt;TCommand&gt;.Executed" /> 事件的处理方法。
        /// <para><paramref name="command"/> 或 <paramref name="commands"/> 必然有一个为 null 值。</para>
        /// </summary>
        /// <param name="type">执行的类型。</param>
        /// <param name="result">操作的返回值。</param>
        /// <param name="command">执行的命令。</param>
        /// <param name="commands">执行命令的集合。</param>
        /// <param name="userToken">用户自定义数据。</param>
        protected override void VirtualExecuted(ExecuteType type, IResult result, TCommand command, List<TCommand> commands, object userToken)
        {
            base.VirtualExecuted(type, result, command, commands, userToken);
            if(this._Manager == null) return;
            if(command != null)
                this._Manager.OnExecuted(type, result, command, userToken);
            else if(commands != null)
                this._Manager.OnExecuted(type, result, commands, userToken);
        }

        /// <summary>
        /// 表示 <see cref="Sofire.Data.ExecuteEventObjectBase&lt;TCommand&gt;.Executing"/> 事件的处理方法。
        /// <para><paramref name="command"/> 或 <paramref name="commands"/> 必然有一个为 null 值。</para>
        /// </summary>
        /// <param name="type">执行的类型。</param>
        /// <param name="command">执行的命令。</param>
        /// <param name="commands">执行的命令的集合。</param>
        protected override object VirtualExecuting(ExecuteType type, TCommand command, List<TCommand> commands)
        {
            var r = base.VirtualExecuting(type, command, commands);
            if(this._Manager != null)
            {
                if(command != null)
                    return this._Manager.OnExecuting(type, command);
                else if(commands != null)
                    return this._Manager.OnExecuting(type, commands);
            }
            return r;
        }

        #endregion Methods
    }
}